#include "MainWindow.h"

#include "RenderDevice.h"
#include "RenderTest.h"
#include "SceneManager.h"

#include <windows.h>
#include <windowsx.h>
#include <ctime>
#include <cstdlib>
#include <mmsystem.h>
#include <iostream>
#include <sstream>
#include <string>

//////////////////////////////////////////////////
//MainWindow
//////////////////////////////////////////////////

//------------------------------------------------

MainWindow::MainWindow(void)
: m_render_test(new RenderTest())
, m_scene_manager(new SceneManager())
  {  
  }

//------------------------------------------------

MainWindow::~MainWindow(void)
  {
  delete m_render_test;
  delete m_scene_manager;
  }

//------------------------------------------------

bool MainWindow::Init(HINSTANCE ih_instance)
  {
  WNDCLASS wc;

	wc.style         = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc   = (WNDPROC)MainWindow::WndProc; 
	wc.cbClsExtra    = 0;
	wc.cbWndExtra    = 0;
	wc.hInstance     = ih_instance;
	wc.hIcon         = LoadIcon(0, IDI_APPLICATION);
	wc.hCursor       = LoadCursor(0, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName  = 0;
	wc.lpszClassName = "SwatGAME";

	if( !RegisterClass(&wc) ) 
	  {
		::MessageBox(0, "RegisterClass() - FAILED", 0, 0);
		return false;
	  }
		
	mh_window = ::CreateWindow("SWATGame", "ETools", WS_EX_TOPMOST, 0, 0, 800, 600, 0, 0, ih_instance, 0); 

	if( !mh_window )
	  {
		::MessageBox(0, "CreateWindow() - FAILED", 0, 0);
		return false;
	  }
	::SetWindowLong(mh_window, GWL_USERDATA, (LONG)this);
	::ShowWindow(mh_window, SW_SHOW);
	::UpdateWindow(mh_window);
  return true;
  }

//------------------------------------------------

HWND MainWindow::GetWindowHandle()
  {
  return mh_window;
  }

//------------------------------------------------

int MainWindow::enterMessageLoop()
  {
	MSG msg;
	::ZeroMemory(&msg, sizeof(MSG));	

  unsigned long currentTime;
  unsigned long lastTime = timeGetTime();
  int current_fps = 0;
  int previous_fps = 0;
  float elapsed;	

	while(msg.message != WM_QUIT)
	  {
		if(::PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
		  {
			::TranslateMessage(&msg);
			::DispatchMessage(&msg);
		  }
		else
      {      
      currentTime = timeGetTime();  
      elapsed = (currentTime - lastTime) * 1.0f;      

      //m_render_test->Render();
      if(m_scene_manager->IsLoaded())
        {
        m_render_device->GetDirect3DDevice()->BeginScene();        
        m_render_device->GetDirect3DDevice()->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);        
        m_scene_manager->RenderScene(m_render_device);        
        if(elapsed > 1000)
          {
          previous_fps = current_fps;
          current_fps = -1;          
          lastTime = timeGetTime();          
          }
        m_render_device->ShowFPS(previous_fps);
        m_render_device->GetDirect3DDevice()->EndScene();
	      m_render_device->GetDirect3DDevice()->Present(0, 0, 0, 0);
        current_fps++;
        }      
      }
    }
  return msg.wParam;        
  }

//------------------------------------------------

LRESULT CALLBACK MainWindow::WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  {
	MainWindow* pWindow = (MainWindow*)GetWindowLong(hwnd, GWL_USERDATA );
	return pWindow->myWndProc(hwnd, msg, wParam, lParam);
  }

//------------------------------------------------

LRESULT CALLBACK MainWindow::myWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  {
	switch( msg )
	  {
	  case WM_DESTROY:
		  ::PostQuitMessage(0);
		  break;	
	  }
	return ::DefWindowProc(hwnd, msg, wParam, lParam);
  }

//------------------------------------------------

void MainWindow::SetRenderDevice(RenderDevice* i_render_device)
  {
  m_render_device = i_render_device;
  m_render_test->SetRenderDevice(i_render_device);
  m_scene_manager->LoadScene("C:\\Users\\janwi\\Desktop\\", "level1.x", i_render_device);
  }

//////////////////////////////////////////////////